﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Win;
using System.Collections.Generic;
using DevExpress.ExpressApp.Updating;
using DevExpress.ExpressApp.Xpo;
using aztall.Module.BusinessObjects;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Actions;
using System.Windows.Forms;
using DevExpress.Data.Filtering;

namespace aztall.Win
{
	// For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/DevExpressExpressAppWinWinApplicationMembersTopicAll.aspx
	public partial class aztallWindowsFormsApplication : WinApplication
	{
		public aztallWindowsFormsApplication()
		{
			InitializeComponent();
			LinkNewObjectToParentImmediately = false;
			this.DelayedViewItemsInitialization = false;
		}
		protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args)
		{
			args.ObjectSpaceProviders.Add(new XPObjectSpaceProvider(args.ConnectionString, args.Connection, false));
			args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
		}
		private void aztallWindowsFormsApplication_CustomizeLanguagesList(object sender, CustomizeLanguagesListEventArgs e)
		{
			string userLanguageName = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
			if (userLanguageName != "en-US" && e.Languages.IndexOf(userLanguageName) == -1)
			{
				e.Languages.Add(userLanguageName);
			}
		}
		private void aztallWindowsFormsApplication_DatabaseVersionMismatch(object sender, DevExpress.ExpressApp.DatabaseVersionMismatchEventArgs e)
		{
#if EASYTEST
            e.Updater.Update();
            e.Handled = true;
#else
			if (System.Diagnostics.Debugger.IsAttached)
			{
				e.Updater.Update();
				e.Handled = true;
			}
			else
			{
				string message = "The application cannot connect to the specified database, " +
					"because the database doesn't exist, its version is older " +
					"than that of the application or its schema does not match " +
					"the ORM data model structure. To avoid this error, use one " +
					"of the solutions from the https://www.devexpress.com/kb=T367835 KB Article.";

				if (e.CompatibilityError != null && e.CompatibilityError.Exception != null)
				{
					message += "\r\n\r\nInner exception: " + e.CompatibilityError.Exception.Message;
				}
				throw new InvalidOperationException(message);
			}
#endif
		}
		protected override void OnLoggedOn(LogonEventArgs args)
		{
			IObjectSpace os = base.CreateObjectSpace();
			//Env.CurrentUser = os.GetObjectByKey<User>(SecuritySystem.CurrentUserId);
			MetricHelper.Init(os);
			StatusHelper.Init(os);
		}
		public override void LogOff()
		{
			//this.Exit();
			base.LogOff();
			//this.Modules.Remove(this.Modules.FindModule<aztall.Module.Win.aztallWindowsFormsModule>());
			//this.Modules.Remove(this.Modules.FindModule<aztall.Module.aztallModule>());
			//this.Setup();
			//	this.Start();

		}
	}

}
